<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;


use app\admin\model\MemberModel;

class Member extends Base
{
    // 角色列表
    public function index()
    {
        if (request()->isAjax()) {

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['role_name'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $return['total'] = MemberModel::count();  // 总数据
            $return['rows'] = MemberModel::where($where)->limit($offset, $limit)->select();
            // 0 普通会员  1 大众会员 2 奥迪会员 3 宝马会员  4 宾利会员 
            foreach ($return['rows'] as $k => &$v) {
                $return['rows'][$k]['operate'] = showOperate($this->makeButton($v['userId']));
                switch ($v['level']) {
                    case 1:
                    $v['showlevel'] = '大众会员';
                        break;
                    case 2:
                    $v['showlevel'] = '奥迪会员';
                        break;
                    case 3:
                    $v['showlevel'] = '宝马会员';
                        break;
                    case 4:
                    $v['showlevel'] = '宾利会员';
                        break;
                    
                    default:
                    $v['showlevel'] = '普通会员';
                        break;
                }
                switch ($v['sex']) {
                    case 1:
                    $v['sex'] = '男';
                        break;
                    case 2:
                    $v['sex'] = '女';
                        break;
                }
                switch ($v['underMembers']) {
                    case 1:
                    $v['underMembers'] = '已开通';
                        break;
                    case 0:
                    $v['underMembers'] = '未开通';
                        break;
                }
            }
            return json($return);
        }
        return $this->fetch();
    }


    public function memberedit(){
        $MemberModel = new MemberModel;
        // 角色列表
        if (request()->isAjax()) {
            $param = input('param.');
            if($param['status'] == 'on'){
                $param['underMembers'] = 1;
            }else{
                $param['underMembers'] = 0;
            }
            $param['startMemberTime'] = $param['startMemberTime'] > 0 ? strtotime($param['startMemberTime']) : null;
            $param['endMemberTime'] = $param['endMemberTime'] > 0 ? strtotime($param['endMemberTime']) : 0;
            $return = $MemberModel->editMember($param);
            // 0 普通会员  1 大众会员 2 奥迪会员 3 宝马会员  4 宾利会员 
            return json(msg($return['code'], $return['data'], $return['msg']));
        }
        $userId = input('param.userId');
        $userInfo = $MemberModel->getMenberData($userId);
        if(!$userInfo['endMemberTime']){
            $userInfo['endMemberTime'] = 9999999999;
        }
        $this->assign('userInfo',$userInfo);
        return $this->fetch();
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'member/memberedit',
                'href' => url('member/memberedit', ['userId' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            // '删除' => [
            //     'auth' => 'member/memberdel',
            //     'href' => "javascript:articleDel(" . $id . ")",
            //     'btnStyle' => 'danger',
            //     'icon' => 'fa fa-trash-o'
            // ]
        ];
    }

}